<?php
/**
 * Created by PhpStorm.
 * User: dingjinlai
 * Date: 2019/3/3
 * Time: 下午4:45
 */


defined('BASEPATH') OR exit('No direct script access allowed');

class ExportPuyuan extends Admin_Controller
{
    public function goods_from() {

        $this->load->model('goods_model');
        $this->load->model('goods_sku_model');


        $assoc = $this->uri->uri_to_assoc();

        $goods_id = intval($assoc['goods_id']);

        $goods_list = $this->goods_sku_model->e_goods_list($goods_id);


        //$data = "操作类型,商品编码,SKU,多款式,是否有样品,样品数量,大类名称,小类名称,商品名称,当前状态,材质,规格,型号,款式,品牌,单位,最小包装数,重量(G),采购渠道,供应商名称,成本单价(元),批发价格(美元),零售价格(美元),最低售价(美元),最高售价(美元),市场参考价(美元),数量,备注,中文申报名,英文申报名,申报价值(美元),原产国代码,原产国,库存上限,库存下限,业绩归属人1,业绩归属人2,包装规格,开发日期,SKU款式1,SKU款式2,SKU款式3,SKU描述,图片URL,采购员,发货仓库,采购到货天数,内包装成本,网页URL,网页URL2,网页URL3,最低采购价格,海关编码,库存预警销售周期,采购最小订货量,内盒长,内盒宽,内盒高,内盒毛重,内盒净重,外箱长,外箱宽,外箱高,外箱毛重,外箱净重,商品URL,包装事项,是否带电,商品SKU状态,工号权限,季节,是否粉末,是否液体,责任归属人1,责任归属人2,店铺名称,UPC码,ASIN码,网页URL4,网页URL5,网页URL6,商品属性,店铺运费,包装材料重量,汇率,物流公司价格,交易费,毛利率,计算售价,包装难度系数\n";
        $data = "产品SKU,产品名称,产品英文名称,产品品类（代码）,规格,参考链接,重量,长,宽,高,海关申报代码,英文申报品名,中文申报品名,申报价值,申报币种,采购价,采购币种,默认供应商代码,销售价格,销售运费,建立原因,供应商产品地址,供应商品号,款式代码,成品,运营方式,贴标容易度,产品销售状态,销售负责人,开发负责人,设计师,自定义分类,是否需要质检,组织机构（代码）,申报说明,是否包含电池,是否为仿制品,最小采购量,交期,中文描述,英文描述,净重,EAN码,产品单位代码,UPC码,材质,产品颜色,尺寸,,产品说明名称,产品说明,产品使用到期日,建议销售价,建议售价币种,英文材质,中文用途,英文用途,采购参考价\n";

        foreach ($goods_list as $item) {

            $item['original_img'] = ($item['shopify_id'] > 0 ? $item['original_img'] : ('http://sumerbikini.com' . $item['original_img']));
            $item['cn_name'] = ($item['cn_name'] ? $item['cn_name'] : $item['cat_name_zh']) . (' ' . $item['color'] . ' / ' . $item['size']);
            $item['suppliers_name'] = trim($item['suppliers_name']);

            if (!$item['suppliers_name'] || !$item['cat_name_zh']) {
                continue;
            }

            $item['goods_weight'] = sprintf('%.4f', $item['goods_weight'] / 1000);

            $declared_value = mt_rand(1,2) . '.' . mt_rand(1, 9);

            //$data .= "add,{$item['sku_sn']},{$item['sku_sn']},否,,,,,{$item['cn_name']},,,," . ($item['color'] . ' / ' . $item['size']) . ",,,,,{$item['goods_weight']},,{$item['suppliers_name']},{$item['in_price']},,{$item['shop_price']},,,,,,{$item['cat_name_zh']},{$item['cat_name']},{$declared_value},CN,CHINA,,,,,,,,,,,{$item['original_img']},,上海松江仓,,,{$item['supp_url']},,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,\n";
            //$data .= "{$item['sku_sn']},{$item['cn_name']},{$item['goods_name']},," . ($item['color'] . ' / ' . $item['size']) . ",,{$item['goods_weight']},,,,,{$item['cat_name']},{$item['cat_name_zh']},2,USD,{$item['in_price']},RMB,{$item['suppliers_name']},{$item['shop_price']},,,{$item['supp_url']},{$item['supp_sn']},,,,,,,,,,,,,,,,,,,,,,,,,{$item['color']},{$item['size']}\n";
            $data .= "{$item['sku_sn']},{$item['cn_name']},{$item['goods_name']},," . ($item['color'] . ' / ' . $item['size']) . ",,{$item['goods_weight']},1,1,1,,{$item['cat_name']},{$item['cat_name_zh']},1.5,USD,{$item['in_price']},RMB,{$item['suppliers_name']},{$item['shop_price']},,,{$item['supp_url']},,,,,,在线产品,,,,,,,,,,,,,,,,,,,,{$item['color']},{$item['size']},{$item['original_img']},,,,,,,,\n";
        }


        $filename = 'export_goods-' . date('YmdHis',  time()).'.csv';
        header("Content-type:text/csv; charset=utf-8");
        header("Content-Disposition: attachment; filename=$filename");
        echo $data;
        exit;
    }

    public function supp_form()
    {
        $this->load->model('suppliers_model');

        $data = "所属类别,编码,停用,名称,简拼,联系人,办公电话,手机,地址,公司账号,网址,QQ/MSN,淘宝旺旺,邮箱,到货天数,备注,1688供应商ID\n";

        $supp = $this->suppliers_model->suppliers_option();

        foreach ($supp as $k => $val) {
            $data .= ",G{$k},,{$val},,,,,,,,,,,,,\n";
        }


        $filename = 'export_supp-' . date('YmdHis',  time()).'.csv';
        header("Content-type:text/csv; charset=utf-8");
        header("Content-Disposition: attachment; filename=$filename");
        echo $data;
        exit;
    }

    public function purchase_form()
    {
        $this->load->model('purchase_goods_model');

        $purchase = $this->purchase_goods_model->purchase_goods_f();
        
        $data = "供应商,SKU,Qty,仓库,备注,合同号,采购员,含税单价,物流费,付款方式,制单人,到货日期,1688单号,预付款\n";

        foreach ($purchase as $item) {

            $item['pg_number'] -= $item['inv_number'];

            $item['suppliers_name'] = trim($item['suppliers_name']);

            $data .= "{$item['suppliers_name']},{$item['sku_sn']},{$item['pg_number']},上海松江仓,,'{$item['po_sn']},,,,,,,'{$item['ali_invoice_no']},\n";
        }

        $filename = 'export_purchase-' . date('YmdHis',  time()).'.csv';
        header("Content-type:text/csv; charset=utf-8");
        header("Content-Disposition: attachment; filename=$filename");
        echo $data;
        exit;
    }

    public function goods_shopify()
    {

       $this->load->model('shopify_goods_model');
       $this->load->model('goods_sku_model');

        $assoc = $this->uri->uri_to_assoc();

        $goods_id = trim($assoc['goods_id']);

        $goods_id = $goods_id ? $goods_id : trim($this->input->get('goods_id'));

        $web_id = trim($assoc['web_id']);

       $shopify_goods = $this->shopify_goods_model->pu_shopify_goods($goods_id,$web_id);
       
       $data = "平台SKU,系统SKU,系统SKU数量,系统SKU单价,仓库代码,平台账号\n";

        foreach ($shopify_goods as $item) {

            $goods_sku = $this->db->from('goods_sku')->select('sku_num_sn,sku_sn')->where('goods_id',$item['goods_id'])->get()->result_array();

            //$variants = unserialize($item['variants']);

            foreach ($goods_sku as $variant) {

                //$sku = substr($variant['sku_num_sn'], 2);

                $sku_sn = str_replace('/','',$variant['sku_sn']);

                $data .= "{$item['ab_name']}{$variant['sku_num_sn']},{$variant['sku_sn']},1,,,\n";
            }
        }


        $filename = 'export_goods_shopify-' . date('YmdHis',  time()).'.csv';
        header("Content-type:text/csv; charset=utf-8");
        header("Content-Disposition: attachment; filename=$filename");
        echo $data;
        exit;
    }

    public function shopify_goods_sn()
    {
        $this->load->model('shopify_goods_model');

        $this->shopify_goods_model->add_goods_sn();
    }
}
